﻿
using LPP.Core.Services;
using LPP.Entity.Booking;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace LPP.API.Controllers.BookingController
{
    [ApiController]
    [Route("api/[controller]")]
    public class BookingController : ControllerBase
    {
        private readonly IBookingService _bookingService;

        public BookingController(IBookingService bookingService)
        {
            _bookingService = bookingService ?? throw new ArgumentNullException(nameof(bookingService));
        }

        [HttpPost("create")]
        public async Task<IActionResult> CreateBooking([FromBody] Booking booking)
        {
            if (booking == null)
            {
                return BadRequest(new { message = "Invalid booking data" });
            }

            var result = await _bookingService.CreateBookingAsync(booking);
            if (result)
            {
                return Ok(new { message = "Booking created successfully" });
            }

            return BadRequest(new { message = "Failed to create booking" });
        }

        [HttpGet("{id}")]
        public async Task<IActionResult> GetBooking(int id)
        {
            if (id <= 0)
            {
                return BadRequest(new { message = "Invalid booking ID" });
            }

            var booking = await _bookingService.GetBookingByIdAsync(id);
            if (booking != null)
            {
                return Ok(booking);
            }

            return NotFound(new { message = "Booking not found" });
        }

        [HttpGet("all")]
        public async Task<IActionResult> GetAllBookings()
        {
            var bookings = await _bookingService.GetAllBookingsAsync();
            if (bookings == null || bookings.Count == 0)
            {
                return NotFound(new { message = "No bookings found" });
            }
            return Ok(bookings);
        }

        [HttpPut("{id}")]
        public async Task<IActionResult> UpdateBooking(int id, [FromBody] Booking bookingDto)
        {
            if (id <= 0 || bookingDto == null)
            {
                return BadRequest(new { message = "Invalid data" });
            }

            var result = await _bookingService.UpdateBookingAsync(id, bookingDto);
            if (result)
            {
                return Ok(new { message = "Booking updated successfully" });
            }

            return BadRequest(new { message = "Failed to update booking" });
        }

        [HttpDelete("{id}")]
        public async Task<IActionResult> CancelBooking(int id)
        {
            if (id <= 0)
            {
                return BadRequest(new { message = "Invalid booking ID" });
            }

            var result = await _bookingService.CancelBookingAsync(id);
            if (result)
            {
                return Ok(new { message = "Booking cancelled successfully" });
            }

            return BadRequest(new { message = "Failed to cancel booking" });
        }
    }
}

